package GameEntities
{
	import com.cheezeworld.entity.Entity;
	import com.cheezeworld.entity.EntityParams;
	import com.cheezeworld.entity.MovingEntity;
	import com.cheezeworld.entity.MovingEntityParams;
	import com.cheezeworld.math.Vector2D;
	import com.cheezeworld.screens.AScreen;
	import com.cheezeworld.screens.IScreenItem;
	import com.cheezeworld.utils.Input;
	import com.cheezeworld.utils.Utils;
	
	import flash.display.Sprite;
	import flash.text.TextField;

	public class EntityDemo extends AScreen
	{
		public function EntityDemo(parentScreen:IScreenItem=null)
		{
			super(parentScreen);
			
			var params:MovingEntityParams = new MovingEntityParams();
			params.boundsBehavior = MovingEntity.BOUNDS_BOUNCE;
			params.radius = 20;
			params.maxSpeed = 100;
			params.friction = .8;
			m_movEnt = new MovingEntity( params );
			m_movEnt.addConstantForce( new Vector2D( 0, 20 ), "gravity" );
			m_movEnt.newPos.x = 300;
			m_movEnt.newPos.y = 300;
			
			m_worldEntity = new Entity( new EntityParams() );
			m_worldEntity.bounds.setSize( 550, 550 );
			m_worldEntity.addChild( m_movEnt );
			
			spr = new Sprite();
			addChild( spr );
			spr.graphics.beginFill( 0x0000FF );
			spr.graphics.drawCircle( 0, 0, m_movEnt.radius );
			
			m_tf = Utils.createTextField( this, 100, 100 );
			m_tf.htmlText = Utils.createHtmlText( "Hold mouse to make ball attract to mouse position.", "#FFFFFF" );
		}
		
		public override function update( timePassed:int ):void
		{
			m_worldEntity.update( timePassed );
			spr.x = m_movEnt.newPos.x;
			spr.y = m_movEnt.newPos.y;
			if( Input.instance.isMouseDown )
			{
				var vectorToTarget:Vector2D = new Vector2D( Input.instance.mouseX, Input.instance.mouseY ).subtractedBy( m_movEnt.actualPos );
				var dist:Number = m_movEnt.actualPos.distanceTo( new Vector2D( Input.instance.mouseX, Input.instance.mouseY ) );
				vectorToTarget.normalize();
				vectorToTarget.multiply( m_movEnt.maxSpeed <= dist ? ( m_movEnt.maxSpeed ) : ( dist ) ); 
				m_movEnt.applyForce( vectorToTarget );
			}
		}
		
		private var m_movEnt:MovingEntity;
		private var m_worldEntity:Entity;
		private var spr:Sprite;
		private var m_tf:TextField;
		
	}
}